<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="姓名或账户" v-model="listQuery.name"> </el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" v-if="commonDeviceManager_btn_add" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="id" width="65">
      <template scope="scope">
        <span>{{scope.row.id}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备编号">
      <template scope="scope">
        <span>{{scope.row.deviceNo}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备物理号">
      <template scope="scope">
        <span>{{scope.row.devicePhyNum}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="绑定日期">
      <template scope="scope">
        <span>{{scope.row.bindDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="绑定人">
      <template scope="scope">
        <span>{{scope.row.bindUser}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="创建时间">
      <template scope="scope">
        <span>{{scope.row.createDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="创建人">
      <template scope="scope">
        <span>{{scope.row.creator}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="入库日期">
      <template scope="scope">
        <span>{{scope.row.instoreDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="总行下发人">
      <template scope="scope">
        <span>{{scope.row.iussedHeadUser}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="分行下发人">
      <template scope="scope">
        <span>{{scope.row.iussedUser}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="更新日期">
      <template scope="scope">
        <span>{{scope.row.modifyDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="签收日期">
      <template scope="scope">
        <span>{{scope.row.signDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="签收人">
      <template scope="scope">
        <span>{{scope.row.signUser}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备状态">
      <template scope="scope">
        <span>{{scope.row.state}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="更新人">
      <template scope="scope">
        <span>{{scope.row.updateUser}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="使用截止日期">
      <template scope="scope">
        <span>{{scope.row.useEndDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="使用区域">
      <template scope="scope">
        <span>{{scope.row.useLocation}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="使用开始日期">
      <template scope="scope">
        <span>{{scope.row.useStartDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="使用人">
      <template scope="scope">
        <span>{{scope.row.user}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="机构号">
      <template scope="scope">
        <span>{{scope.row.branchId}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备下发的分行或直属支行">
      <template scope="scope">
        <span>{{scope.row.handBranchId}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备上一次状态">
      <template scope="scope">
        <span>{{scope.row.lastState}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="使用开始时间">
      <template scope="scope">
        <span>{{scope.row.useStartTime}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="使用截止时间">
      <template scope="scope">
        <span>{{scope.row.useEndTime}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="所属条线">
      <template scope="scope">
        <span>{{scope.row.level}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备类型">
      <template scope="scope">
        <span>{{scope.row.deviceType}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备来源机构">
      <template scope="scope">
        <span>{{scope.row.fromBranchId}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="设备激活状态">
      <template scope="scope">
        <span>{{scope.row.activateState}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="发卡机模式">
      <template scope="scope">
        <span>{{scope.row.cardPattern}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="业务开始时间">
      <template scope="scope">
        <span>{{scope.row.businessStartTime}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="业务结束时间">
      <template scope="scope">
        <span>{{scope.row.businessEndTime}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="授权模式">
      <template scope="scope">
        <span>{{scope.row.authorizeMode}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="UDID唯一标识">
      <template scope="scope">
        <span>{{scope.row.udid}}</span>
      </template>
    </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="150"> <template scope="scope">
        <el-button v-if="commonDeviceManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
        </el-button>
        <el-button v-if="commonDeviceManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
        </el-button>
      </template> </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="设备编号" prop="deviceNo">
      <el-input v-model="form.deviceNo" placeholder="请输入设备编号"></el-input>
    </el-form-item>
        <el-form-item label="设备物理号" prop="devicePhyNum">
      <el-input v-model="form.devicePhyNum" placeholder="请输入设备物理号"></el-input>
    </el-form-item>
        <el-form-item label="绑定日期" prop="bindDate">
      <el-input v-model="form.bindDate" placeholder="请输入绑定日期"></el-input>
    </el-form-item>
        <el-form-item label="绑定人" prop="bindUser">
      <el-input v-model="form.bindUser" placeholder="请输入绑定人"></el-input>
    </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="form.createDate" placeholder="请输入创建时间"></el-input>
    </el-form-item>
        <el-form-item label="创建人" prop="creator">
      <el-input v-model="form.creator" placeholder="请输入创建人"></el-input>
    </el-form-item>
        <el-form-item label="入库日期" prop="instoreDate">
      <el-input v-model="form.instoreDate" placeholder="请输入入库日期"></el-input>
    </el-form-item>
        <el-form-item label="总行下发人" prop="iussedHeadUser">
      <el-input v-model="form.iussedHeadUser" placeholder="请输入总行下发人"></el-input>
    </el-form-item>
        <el-form-item label="分行下发人" prop="iussedUser">
      <el-input v-model="form.iussedUser" placeholder="请输入分行下发人"></el-input>
    </el-form-item>
        <el-form-item label="更新日期" prop="modifyDate">
      <el-input v-model="form.modifyDate" placeholder="请输入更新日期"></el-input>
    </el-form-item>
        <el-form-item label="签收日期" prop="signDate">
      <el-input v-model="form.signDate" placeholder="请输入签收日期"></el-input>
    </el-form-item>
        <el-form-item label="签收人" prop="signUser">
      <el-input v-model="form.signUser" placeholder="请输入签收人"></el-input>
    </el-form-item>
        <el-form-item label="设备状态" prop="state">
      <el-input v-model="form.state" placeholder="请输入设备状态"></el-input>
    </el-form-item>
        <el-form-item label="更新人" prop="updateUser">
      <el-input v-model="form.updateUser" placeholder="请输入更新人"></el-input>
    </el-form-item>
        <el-form-item label="使用截止日期" prop="useEndDate">
      <el-input v-model="form.useEndDate" placeholder="请输入使用截止日期"></el-input>
    </el-form-item>
        <el-form-item label="使用区域" prop="useLocation">
      <el-input v-model="form.useLocation" placeholder="请输入使用区域"></el-input>
    </el-form-item>
        <el-form-item label="使用开始日期" prop="useStartDate">
      <el-input v-model="form.useStartDate" placeholder="请输入使用开始日期"></el-input>
    </el-form-item>
        <el-form-item label="使用人" prop="user">
      <el-input v-model="form.user" placeholder="请输入使用人"></el-input>
    </el-form-item>
        <el-form-item label="机构号" prop="branchId">
      <el-input v-model="form.branchId" placeholder="请输入机构号"></el-input>
    </el-form-item>
        <el-form-item label="设备下发的分行或直属支行" prop="handBranchId">
      <el-input v-model="form.handBranchId" placeholder="请输入设备下发的分行或直属支行"></el-input>
    </el-form-item>
        <el-form-item label="设备上一次状态" prop="lastState">
      <el-input v-model="form.lastState" placeholder="请输入设备上一次状态"></el-input>
    </el-form-item>
        <el-form-item label="使用开始时间" prop="useStartTime">
      <el-input v-model="form.useStartTime" placeholder="请输入使用开始时间"></el-input>
    </el-form-item>
        <el-form-item label="使用截止时间" prop="useEndTime">
      <el-input v-model="form.useEndTime" placeholder="请输入使用截止时间"></el-input>
    </el-form-item>
        <el-form-item label="所属条线" prop="level">
      <el-input v-model="form.level" placeholder="请输入所属条线"></el-input>
    </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
      <el-input v-model="form.deviceType" placeholder="请输入设备类型"></el-input>
    </el-form-item>
        <el-form-item label="设备来源机构" prop="fromBranchId">
      <el-input v-model="form.fromBranchId" placeholder="请输入设备来源机构"></el-input>
    </el-form-item>
        <el-form-item label="设备激活状态" prop="activateState">
      <el-input v-model="form.activateState" placeholder="请输入设备激活状态"></el-input>
    </el-form-item>
        <el-form-item label="发卡机模式" prop="cardPattern">
      <el-input v-model="form.cardPattern" placeholder="请输入发卡机模式"></el-input>
    </el-form-item>
        <el-form-item label="业务开始时间" prop="businessStartTime">
      <el-input v-model="form.businessStartTime" placeholder="请输入业务开始时间"></el-input>
    </el-form-item>
        <el-form-item label="业务结束时间" prop="businessEndTime">
      <el-input v-model="form.businessEndTime" placeholder="请输入业务结束时间"></el-input>
    </el-form-item>
        <el-form-item label="授权模式" prop="authorizeMode">
      <el-input v-model="form.authorizeMode" placeholder="请输入授权模式"></el-input>
    </el-form-item>
        <el-form-item label="UDID唯一标识" prop="udid">
      <el-input v-model="form.udid" placeholder="请输入UDID唯一标识"></el-input>
    </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
      page,
      addObj,
      getObj,
      delObj,
      putObj
  } from 'api/common/commonDevice/index';
  import { mapGetters } from 'vuex';
  export default {
    name: 'commonDevice',
    data() {
      return {
        form: {
        deviceNo : undefined,        devicePhyNum : undefined,        bindDate : undefined,        bindUser : undefined,        createDate : undefined,        creator : undefined,        instoreDate : undefined,        iussedHeadUser : undefined,        iussedUser : undefined,        modifyDate : undefined,        signDate : undefined,        signUser : undefined,        state : undefined,        updateUser : undefined,        useEndDate : undefined,        useLocation : undefined,        useStartDate : undefined,        user : undefined,        branchId : undefined,        handBranchId : undefined,        lastState : undefined,        useStartTime : undefined,        useEndTime : undefined,        level : undefined,        deviceType : undefined,        fromBranchId : undefined,        activateState : undefined,        cardPattern : undefined,        businessStartTime : undefined,        businessEndTime : undefined,        authorizeMode : undefined,        udid : undefined          },
        rules: {
    deviceNo: [
  {
    required: true,
    message: '请输入设备编号',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   devicePhyNum: [
  {
    required: true,
    message: '请输入设备物理号',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   bindDate: [
  {
    required: true,
    message: '请输入绑定日期',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   bindUser: [
  {
    required: true,
    message: '请输入绑定人',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   createDate: [
  {
    required: true,
    message: '请输入创建时间',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   creator: [
  {
    required: true,
    message: '请输入创建人',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   instoreDate: [
  {
    required: true,
    message: '请输入入库日期',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   iussedHeadUser: [
  {
    required: true,
    message: '请输入总行下发人',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   iussedUser: [
  {
    required: true,
    message: '请输入分行下发人',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   modifyDate: [
  {
    required: true,
    message: '请输入更新日期',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   signDate: [
  {
    required: true,
    message: '请输入签收日期',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   signUser: [
  {
    required: true,
    message: '请输入签收人',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   state: [
  {
    required: true,
    message: '请输入设备状态',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   updateUser: [
  {
    required: true,
    message: '请输入更新人',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   useEndDate: [
  {
    required: true,
    message: '请输入使用截止日期',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   useLocation: [
  {
    required: true,
    message: '请输入使用区域',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   useStartDate: [
  {
    required: true,
    message: '请输入使用开始日期',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   user: [
  {
    required: true,
    message: '请输入使用人',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   branchId: [
  {
    required: true,
    message: '请输入机构号',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   handBranchId: [
  {
    required: true,
    message: '请输入设备下发的分行或直属支行',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   lastState: [
  {
    required: true,
    message: '请输入设备上一次状态',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   useStartTime: [
  {
    required: true,
    message: '请输入使用开始时间',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   useEndTime: [
  {
    required: true,
    message: '请输入使用截止时间',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   level: [
  {
    required: true,
    message: '请输入所属条线',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   deviceType: [
  {
    required: true,
    message: '请输入设备类型',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   fromBranchId: [
  {
    required: true,
    message: '请输入设备来源机构',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   activateState: [
  {
    required: true,
    message: '请输入设备激活状态',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   cardPattern: [
  {
    required: true,
    message: '请输入发卡机模式',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   businessStartTime: [
  {
    required: true,
    message: '请输入业务开始时间',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   businessEndTime: [
  {
    required: true,
    message: '请输入业务结束时间',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   authorizeMode: [
  {
    required: true,
    message: '请输入授权模式',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   udid: [
  {
    required: true,
    message: '请输入UDID唯一标识',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
]        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          name: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        commonDeviceManager_btn_edit: false,
        commonDeviceManager_btn_del: false,
        commonDeviceManager_btn_add: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0
      }
    },
    created() {
      this.getList();
      this.commonDeviceManager_btn_edit = this.elements['commonDeviceManager:btn_edit'];
      this.commonDeviceManager_btn_del = this.elements['commonDeviceManager:btn_del'];
      this.commonDeviceManager_btn_add = this.elements['commonDeviceManager:btn_add'];
    },
    computed: {
      ...mapGetters([
        'elements'
      ])
    },
    methods: {
      getList() {
        this.listLoading = true;
        page(this.listQuery)
            .then(response => {
          this.list = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      })
      },
      handleFilter() {
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleCreate() {
        this.resetTemp();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleUpdate(row) {
        getObj(row.id)
            .then(response => {
          this.form = response.data;
        this.dialogFormVisible = true;
        this.dialogStatus = 'update';
      });
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
            .then(() => {
          delObj(row.id)
      .then(() => {
          this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        });
        const index = this.list.indexOf(row);
        this.list.splice(index, 1);
      });
      });
      },
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
        if (valid) {
        addObj(this.form)
        .then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
        title: '成功',
        message: '创建成功',
        type: 'success',
        duration: 2000
        });
        })
        } else {
        return false;
        }
        });
      },
      cancel(formName) {
        this.dialogFormVisible = false;
        const set = this.$refs;
        set[formName].resetFields();
      },
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
        if (valid) {
        this.dialogFormVisible = false;
        this.form.password = undefined;
        putObj(this.form.id, this.form).then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
        title: '成功',
        message: '创建成功',
        type: 'success',
        duration: 2000
        });
        });
        } else {
        return false;
        }
        });
      },
      resetTemp() {
        this.form = {
          username: undefined,
          name: undefined,
          sex: '男',
          password: undefined,
          description: undefined
        };
      }
    }
  }
</script>
